package model.entity;

import model.Coordinate;
import graphic.Assets;
import graphic.drawables.Sheet;

/**
 * Versione di {@link AnimatedEntity2D} basata pero' sugli sheet per le animazioni.
 * @author Jan
 *
 */
public abstract class SheetBasedAnimatedEntity2D extends Entity2D {
	
	private Sheet sheet;
	
	private int index;
	private boolean autoUpdate = true;
	private long updateDelay;
	private long lastUpdate;
	
	public SheetBasedAnimatedEntity2D(int entityID, Coordinate coordinate, int shiftAmount) {
		super(entityID, coordinate, shiftAmount);
		//Deafault...
		sheet = Assets.getsLoader().getSheet(Assets.SHEET_LOADING);
	}
	
	@Override
	public boolean useSheet() {
		return true;
	}
	
	/**
	 * Permette di settare lo sheet.
	 * @param sheet
	 */
	public void setAnimationSheet(Sheet sheet){
		this.sheet = sheet;
	}
	
	public Sheet getSheet() {
		return sheet;
	}
	
	
	public void setAutoUpdate(boolean autoUpdate) {
		this.autoUpdate = autoUpdate;
	}
	
	public  void setUpdateDelay(long updateDelay){
		this.updateDelay = updateDelay;
	}
	
	public void updateFrame(){
		if(index+1 < sheet.getFrameCount()){
			index++;
		}else{
			index = 0;
		}
		lastUpdate = System.currentTimeMillis();
	}
	
	public int[] getFrame(int index) {
		return sheet.getFramePixels(index);
	}
	
	public int[] getFrame(){
		int[] f = sheet.getFramePixels(index);
		if(autoUpdate){
			long now = System.currentTimeMillis();
			if(now-lastUpdate >= updateDelay){
				updateFrame();
			}
		}
		return f;
	}
}
